package com.uchengwang.ui.cm_ui.ucity.adapter;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.uchengwang.R;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.cm_ui.cycle.adapter.BaseCycleAdapter;
import com.uchengwang.ui.cm_ui.cycle.viewHolder.BaseViewHolder;
import com.uchengwang.ui.cm_ui.login.bean.UserBean;
import com.uchengwang.ui.cm_ui.me.presenter.HttpMeManager;
import com.uchengwang.ui.utils.UCommonUtil;
import com.uchengwang.ui.utils.WidgetUtil;

/**
 * Created by U城网 on 2018/2/9.
 */

public class MobileAdapter extends BaseCycleAdapter<UserBean, RecyclerView.ViewHolder> {
    private Context context;

    public MobileAdapter(Context context){
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_layout_view, parent, false);
        return new BaseViewHolder(view, 0);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder h, final int position) {
        final BaseViewHolder holder = (BaseViewHolder) h;
        final UserBean bean = list.get(position);
        final boolean isFriend = bean.isFriend();//是否是好友/是否邀请
        final boolean isFollow = bean.isFollow();//是否关注

        String headUrl = bean.getUserAvatar();
        if (TextUtils.isEmpty(headUrl)){
            ImageManager.getInstance().loadImage(context, holder.head, R.drawable.no_head, ImageManager.ImageType.CIRCLE);
        }else {
            ImageManager.getInstance().loadImage(context, holder.head, headUrl, ImageManager.ImageType.CIRCLE);
        }

        String remark = bean.getRemarkName();
        String nickName = bean.getNickName();
        if (TextUtils.isEmpty(remark)){
            if (!TextUtils.isEmpty(nickName)){
                holder.des.setText(nickName);
            }else {
                holder.des.setText("暂未注册");
            }
        }else {
            holder.des.setText(remark);
        }

        holder.name.setText(bean.getRealName());

        if (bean.getId() >0){
            if (bean.getId() == UCommonUtil.getUtil().getId()){
                holder.bt.setVisibility(View.GONE);
            }else {
                holder.bt.setVisibility(View.VISIBLE);
                if (isFriend || isFollow){
                    //是好友关系
//                    WidgetUtil.getUtil().setChatWidget(holder.bt);
                    holder.bt.setText("已关注");
                    holder.bt.setTextColor(UChengApplication.getContext().getResources().getColor(R.color.delete_selector));
                    holder.bt.setBackgroundResource(0);
                }else {
                    WidgetUtil.getUtil().setAttenWidget(holder.bt, isFollow);
                }
            }
        }else {
            Log.d("jjf", "id=="+bean.getId()+"==姓名=="+bean.getRealName());
            if (isFollow){
                holder.bt.setText("已邀请");
                holder.bt.setBackgroundResource(R.drawable.selector_no_atten);
                holder.bt.setTextColor(context.getResources().getColor(R.color.delete_selector));
            }else {
                holder.bt.setText("邀请");
                holder.bt.setBackgroundResource(R.drawable.selector_friends);
                holder.bt.setTextColor(UChengApplication.getContext().getResources().getColor(R.color.register_user_selector));
            }
        }

        holder.bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (bean.getId() <1){
                    //未注册 点击邀请注册
                    HttpMeManager.getPresenter().inviteRgister((Activity) context, bean.getMobile(), new OnResultListener() {
                        @Override
                        public void onResult(int stutas, String msg, int total, String data) {
                            if (stutas ==0){
                                UCommonUtil.getUtil().showToast(context, "邀请成功");
                                bean.setFollow(true);
                                notifyDataSetChanged();
                            }else {
                                UCommonUtil.getUtil().showToast(context, "邀请失败");
                            }
                        }
                    });

                }else {
                    if (!bean.isFriend() && !bean.isFollow()){
//                        UCommonUtil.getUtil().toChatActiviy(context, bean.getId(), Conversation.ConversationType.PRIVATE, bean.getNickName());
                        follow(holder.bt, bean);
                    }else{//关注/取消关注
                        //已是好友 或者已关注 不能再操作
                    }
                }

            }
        });

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (bean.getId() <1){
                    //未注册
//                    UCommonUtil.getUtil().showToast(context, "邀请注册");
                }else {
                    UCommonUtil.getUtil().toUserActivity((Activity) context, bean.getId());
                }
            }
        });
    }

    /**
     * 关注/取消关注
     * */
    private void follow(final Button bt, final UserBean bean){
        HttpMeManager.getPresenter().addAtten((Activity) context, !bean.isFollow(), bean.getId(), new HttpMeManager.AttenListener() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void atten(int status) {
                if (status != 0){
                    UCommonUtil.getUtil().showToast(context, "操作失败");
                    return;
                }
                if (status == 0){
                    if (bean.isFollow()){
                        UCommonUtil.getUtil().showToast(context, "取消关注成功");
                        bean.setFollow(false);
                        WidgetUtil.getUtil().setAttenWidget(bt, false);
                    }else {
                        UCommonUtil.getUtil().showToast(context, "关注成功");
                        bean.setFollow(true);
                        bt.setTextColor(UChengApplication.getContext().getResources().getColor(R.color.delete_selector));
                        bt.setText("已关注");
                        bt.setBackgroundResource(0);
                    }
                }


            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
}
